/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/


#include "DebugVariableTracker.h"
#include "../../XPropertys\PropertyFactory.h"

namespace XF
{
	DebugVariableTracker *DebugVariableTracker::smInstance = NULL;

	Event1<void, std::string> DebugVariableTracker::OnVariableRegisteredEvent;
	Event2<void, std::string, std::string> DebugVariableTracker::OnVariableChangedEvent;
	Event1<void, std::string> DebugVariableTracker::OnVariableUnregisteredEvent;

	DebugVariableTracker::DebugVariableTracker(Application *ownerApp) : SystemComponent(ownerApp)
	{
		smInstance = this;
	}

	DebugVariableTracker::~DebugVariableTracker()
	{
		smInstance = NULL;
	}

	bool DebugVariableTracker::OnInitialise()
	{		
		return (smInstance == NULL);
	}

	void DebugVariableTracker::OnShutDown()
	{

	}

	void DebugVariableTracker::RegisterVariable( std::string typeID, std::string variableName )
	{
		if(smInstance)
		{
			smInstance->AddProperty(PropertyFactory::CreateProperty(typeID, variableName));
			DebugVariableTracker::OnVariableRegisteredEvent.Invoke(variableName);
		}
		else
		{
			ILOG << "ERROR : DebugVariableTracker::RegisterVariable, instance did not exist";
		}
	}

	void DebugVariableTracker::UpdateVariable( std::string name, std::string value )
	{
		if(smInstance->HasProperty(name)) //chris i added this error check
		{
			smInstance->GetIProperty(name)->SetFromString(value);
			DebugVariableTracker::OnVariableChangedEvent.Invoke(name, value);
		}else
		{
			ILOG << "ERROR - DebugVariableTracker::UpdateVariable - Attempting to update a variable: " << name << " that doesn't exist";
		}
		
	}

	void DebugVariableTracker::UnregisterVariable( std::string name )
	{
		smInstance->RemoveProperty(name);
		DebugVariableTracker::OnVariableUnregisteredEvent.Invoke(name);
	}
}
